package fragment.classicfragments;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ClassifoImgResult;
import fragment.AbsBaseFragment;
import fragment.recommendfragments.ReCommendDetailsFragment;
import fragment.tabhostFragment.SearchFragment;
import tools.ItemOnClickListener;
import utils.adapter.ClassInfoImgRecycerAdapter;
import volley.VolleySingleton;


/**
 * 分类等有声书点击进入的界面
 * Created by dllo on 15/11/20.
 */
public class ClassificationPageClassIfoImg extends AbsBaseFragment implements View.OnClickListener, ItemOnClickListener {
    private RecyclerView recyclerView;
    private ClassInfoImgRecycerAdapter classInfoImgRecycerAdapter;

    private RelativeLayout relativeLayout;
    private TextView classIfoImg_title;
    private ImageView classIfoImg_search,classIfoImg_back;
    private long id;
    private String title;

    public ClassificationPageClassIfoImg(long id, String title) {
        this.id = id;
        this.title = title;
    }


    @Override
    protected int getLayoutView() {
        return R.layout.classifoimg_classificationpage;
    }

    @Override
    protected void initView() {
        relativeLayout = findView(R.id.relativeLayout);
        recyclerView = findView(R.id.recyclerView);
        classIfoImg_title = findView(R.id.classIfoImg_title);
        classIfoImg_back = findView(R.id.classIfoImg_back);
        classIfoImg_title.setText(title);

        classIfoImg_search = findView(R.id.classIfoImg_search);
        classIfoImg_back.setOnClickListener(this);
        classIfoImg_search.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        GridLayoutManager gm = new GridLayoutManager(getContext(), 1);
        recyclerView.setLayoutManager(gm);
        classInfoImgRecycerAdapter = new ClassInfoImgRecycerAdapter(getChildFragmentManager());
        recyclerView.setAdapter(classInfoImgRecycerAdapter);
        classIfoImg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        classInfoImgRecycerAdapter.setItemOnClick(this);
//        classInfoImgRecycerAdapter.setIposition(new ItempostionOnclick() {

        initNetwork();
    }

    private void initNetwork() {
        String url = "http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=" + id + "&contentType=album&device=android&scale=2&version=4.3.26.2";
        VolleySingleton.getInstance().addRequest(url,
                ClassifoImgResult.class, new Response.Listener<ClassifoImgResult>() {
                    @Override
                    public void onResponse(ClassifoImgResult response) {

                        if (response != null) {
                            ClassifoImgResult.FocusImagesEntity fe = response.getFocusImages();

                            if (fe != null) {
                                List<ClassifoImgResult.FocusImagesEntity.ListEntity> list = fe.getList();
                                if (list != null) {
                                    dismissDialog();
                                }
                                classInfoImgRecycerAdapter.addViewPagerData(list);
                            }
                            ClassifoImgResult.CategoryContentsEntity cce = response.getCategoryContents();
                            if (cce != null) {
                                List<ClassifoImgResult.CategoryContentsEntity.ListEntity> liste = cce.getList();
                                if (liste != null) {
                                    classInfoImgRecycerAdapter.addData(liste);
                                }
                            }

                            ClassifoImgResult.TagsEntity tagsEntity = response.getTags();
                            if (tagsEntity != null) {
                                List<ClassifoImgResult.TagsEntity.ListEntity> tagList = tagsEntity.getList();
                                if (tagList != null) {
                                    classInfoImgRecycerAdapter.addFootItemData(tagList);
                                }
                            }
                        }
                    }


                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.classIfoImg_search:
                goToNext(new SearchFragment());
                break;
            case R.id.classIfoImg_back:
                goBack();
                break;
        }
    }


    @Override
    public void itemListener(Object o) {
        ClassifoImgResult.CategoryContentsEntity.ListEntity.ListTEntity li =
                (ClassifoImgResult.CategoryContentsEntity.ListEntity.ListTEntity) o;
        goToNext(new ReCommendDetailsFragment(li.getAlbumId(),1));
    }
}
